package cn.yitun.chat.yitunservermanagecore.general.manage.index.service.impl;

import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.SecurityQuestionDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.system.user.UserInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import cn.yitun.chat.yitunservermanagecore.general.manage.core.manager.SecurityQuestionManager;
import cn.yitun.chat.yitunservermanagecore.general.manage.core.manager.UserManager;
import cn.yitun.chat.yitunservermanagecore.general.manage.index.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账号相关业务
 * Date 2019-01-20 12:35:32
 * Description
 * @author yitun
 * @version 1.1
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

	@Resource
	private UserDao userDao;

	@Resource
	private UserManager userManager;
	@Resource
	private SecurityQuestionManager securityQuestionManager;

	/**
	 * 账号是否存在
	 * @param userId
	 * @param account
	 * @return
	 */
	public boolean isExistAccount(Long userId, String account) {

		UserInfo u = userDao.getUserByAccount(account);
		if (userId == null)
			return null != u;
		return null != u && u.getId().equals(userId);
	}

	/**
	 * 邮箱是否存在
	 * @param userId
	 * @param email
	 * @return
	 */
	public boolean isExistEmail(Long userId, String email) {
		UserInfo u = userDao.getUserByEmail(email);
		if (userId == null)
			return null != u;
		return null != u && u.getId().equals(userId);
	}

	/**
	 * 手机号是否存在
	 * @param userId
	 * @param mobile
	 * @return
	 */
	public boolean isExistMobile(Long userId, String mobile) {
		UserVo u = userDao.getUserVOByMobile(mobile);
		if (userId == null)
			return null != u;
		return null != u && u.getId().equals(userId);
	}

	@Transactional
	@Override
	public Info register(UserDto user, List<SecurityQuestionDto> list) {
		Info info = userManager.add(user);
		if (info.isSuccess()) {
			securityQuestionManager.save(list, user.getId());
		}
		return info;
	}
}
